package com.lagou.controller;

import com.lagou.pojo.Article;
import com.lagou.repository.ArticleRepository;
import com.lagou.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class IndexController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping(value = "/articleList")
    public String articleList(HttpServletRequest request, HttpServletResponse response) {
        String pageNo1 = request.getParameter("pageNo");
        System.out.println(pageNo1);
        if (StringUtils.isEmpty(pageNo1)){
            pageNo1 = "0";
        }
        int pageNo = Integer.parseInt(pageNo1);
        if (pageNo < 0) {
            pageNo = 0;
        }
        Page<Article> articleList = articleService.getArticleList(pageNo);
        request.setAttribute("articleList", articleList);
        return "/client/index";
    }

}
